package com.gg.jdbc.generate;

import com.gg.jdbc.DB2Connection;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class SysColumns {
	
	protected static final String DELIM_LINE = System.getProperty("line.separator");
	
	protected Connection connection;
	
	protected ResultSet rs;
	
	protected ResultSetMetaData data;
	
	protected String table;
	protected String creator;
	
	protected String[] names;
	
	public SysColumns(Connection connection, String creator, String table) {
		this.connection = connection;
		this.creator    = creator;
		this.table      = table;
	}
	
	public boolean execSQL() {
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT NAME FROM SYSIBM.SYSCOLUMNS ");
		sb.append("  WHERE TBNAME    = ? ");
		sb.append("    AND TBCREATOR = ? ");
		sb.append("  ORDER BY COLNO ");
		
		try {
			PreparedStatement s = connection.prepareStatement(sb.toString());
			s.setString(1, table);
			s.setString(2, creator);
			
			rs      = s.executeQuery();
			data    = rs.getMetaData();
			getResultData();
			
			s.close();
			
			return true;
		
		} catch (SQLException e) {
			DB2Connection.sqlException(e);
		}
		return false;
	}

	protected void getResultData() throws SQLException {
		creator = null;
		List<String> list = new ArrayList<String>();
		
		while (rs.next()) {
			list.add(rs.getString("NAME"));
		}
		names = (String[]) list.toArray(new String[list.size()]);
	}
	
	public ResultSet getResultSet() {
		return rs;
	}
	
	public ResultSetMetaData getResultSetMetaData() {
		return data;
	}
	
	public String getTableCreator() {
		return creator;
	}
	
	public String[] getTableColumns() {
		return names;
	}
	
	public String getSelectFormattedSysTableColumns() {
		StringBuffer sb = new StringBuffer();
		try {
			for (int i = 1; i <= data.getColumnCount(); i++) {
				if (i > 1) sb.append(", ");
				sb.append(data.getColumnName(i));
			}
			return sb.toString();
		} catch (SQLException e) {
			DB2Connection.sqlException(e);
		}
		return null;
	}
	
	public String getSelectFormattedTableColumns() {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < names.length; i++) {
			if (i > 0) sb.append(", ");
			sb.append(names[i]);
		}
		return sb.toString();
	}
	
	public StringBuffer getStringBufferSelect() {
		StringBuffer sb = new StringBuffer();
		String lineStart = "\t\tsb.append(\"";
		String lineEnd   = " \");";
		int pos;
		int lineLength = 50;
		
		sb.append("\t\tStringBuffer sb = new StringBuffer();");
		sb.append(DELIM_LINE);
		sb.append(lineStart);
		sb.append("SELECT ");
		pos = lineStart.length() + lineEnd.length() + 7;
		for (int i = 0; i < names.length; i++) {
			if (i > 0) {
				pos += 2;
				sb.append(", ");
			}
			pos += names[i].length();
			sb.append(names[i]);
			if ((pos >= lineLength) && (i < (names.length - 1))) {
				sb.append(lineEnd);
				sb.append(DELIM_LINE);
				sb.append(lineStart);
				sb.append("    ");
				pos = lineStart.length() + lineEnd.length() + 4;
			}
		}
		sb.append(lineEnd);
		sb.append(DELIM_LINE);
		
		sb.append(lineStart);
		sb.append("  FROM");
		sb.append(lineEnd);
		sb.append(DELIM_LINE);
		
		sb.append("\t\tsb.append(");
		sb.append("creator");
		sb.append(");");
		sb.append(DELIM_LINE);
		
		sb.append(lineStart);
		sb.append(".");
		sb.append(table);
		sb.append(lineEnd);
		sb.append(DELIM_LINE);
		
		return sb;
	}

}
